package com.ykw.parking.controller;

import com.ykw.parking.mapper.OrderMapper;
import com.ykw.parking.mapper.OwnerMapper;
import com.ykw.parking.mapper.UserMapper;
import com.ykw.parking.pojo1.ParkingOne;
import com.ykw.parking.pojo1.RealUser;
import com.ykw.parking.pojo1.User;
import com.ykw.parking.pojo1.UserOwner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class OwnerController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    OwnerMapper ownerMapper;
    @Autowired
    OrderMapper orderMapper;
    @GetMapping("/owner")
    public String owner(HttpSession session, Model model){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        RealUser realUser = orderMapper.queryUser(user.getId());
        UserOwner userOwner = ownerMapper.queryOwnerByUid(user.getId());
        if(realUser==null){
            model.addAttribute("msg","请先实名认证在申请业主省份");
        }else if(userOwner!=null){
            if(userOwner.getState()==1){
                model.addAttribute("msg","您已成功业主身份认证");
            }else{
                model.addAttribute("msg","业主身份待审核");
            }

        }else {
            model.addAttribute("msg", "请业主身份认证");
        }
        return "owner";
    }

    @PostMapping("/addOwner")
    @ResponseBody
    public String addOwner(String rnumber,HttpSession session){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        UserOwner userOwner = ownerMapper.queryOwnerByUid(user.getId());
        if(userOwner!=null){
            if(userOwner.getState()==1){
                return "您已成功业主身份认证";
            }else{
                return "业主身份待审核";
            }
        }
        int i = ownerMapper.AddOwner(new UserOwner(user.getId(), user.getPhone_number(),
                "浙江省杭州市", rnumber, 0));
        if(i>0){
            return "添加成功";
        }else{
            return "添加失败";
        }

    }
}
